<?php defined("MAIN_DIR") or die("No direct access");

if ($route->getPath(4) != "") {
	require_once BANKS."vacancy.php";
	die;
}

$vacancy_query = "select * from `vacancy` where `bank_id` in (select `bank_id` from `bank` where `city_id` = '$_SESSION[city]')";
$vacancy_res = mysql_query($vacancy_query);
$all_vacancy_count = mysql_num_rows($vacancy_res);

$category_query = "select `VC`.`category_id` as `id`,`VC`.`title` as `name`,COUNT(`vacancy`.`vacancy_id`) as `count` from `vacancy_categories` as `VC`,`vacancy` where `VC`.`category_id` = `vacancy`.`category_id` and `vacancy`.`bank_id` in (select `bank_id` from `bank` where `city_id` = '$_SESSION[city]') group by `id` order by `name`";
$link = "/$c_name/banks/vacancies/?sort=list&cat_id=";

$sort = "";

if (isset($_GET['sort']) && $_GET['sort'] != "")
{
	$sort = trim(strip_tags($_GET['sort']));
	if ($sort == "bank")
	{
		$category_query = "select `bank`.`bank_id` as `id`,`bank`.`name` as `name`,COUNT(`vacancy`.`vacancy_id`) as `count` from `bank`,`vacancy` where `bank`.`city_id` = '$_SESSION[city]' and `bank`.`bank_id` = `vacancy`.`bank_id` group by `id` order by `name`";
		$link = "/$c_name/banks/vacancies/?sort=list&bank_id=";
	}
	elseif ($sort == "list")
	{
		$category_query = "select `vacancy_id` as `id`,`title` as `name`,`salary` as `count` from `vacancy` where `bank_id` in (select `bank_id` from `bank` where `city_id` = '$_SESSION[city]')";
		if (isset($_GET['cat_id']) && $_GET['cat_id'] != "")
		{
			$cat_id = trim(strip_tags($_GET['cat_id']));
			if (is_numeric($cat_id))
				$category_query .= " and `category_id` = '$cat_id'";
		}
		elseif ($route->GET('bank_id') != "")
		{
			$bank_id = trim(strip_tags($route->GET('bank_id')));
			if (is_numeric($bank_id))
				$category_query .= " and `bank_id` = '$bank_id'";
		}
		$category_query .= " order by `name`";
		$link = "/$c_name/banks/vacancies/";
	}
}

$category_res = mysql_query($category_query);
$limit = 20;
$count = mysql_num_rows($category_res);
$page = 1;
if (isset($_GET['page']))
	$page = $_GET['page'];
$max_page = ceil($count/$limit);
if ($max_page == 0)
	$max_page++;
if ($page > $max_page)
	$page = $max_page;
elseif ($page < 1)
	$page = 1;
$start_list = (ceil($page)-1)*$limit;
$category_query .= " limit $start_list,$limit";
$category_res = mysql_query($category_query);
$category_count = mysql_num_rows($category_res);

$head_title = "Работа в банках ".$current_city['city_name_rod'].". Список вакансий.";
$head_description = "Список банковских вакансий в ".$current_city['city_name_pred'];
$head_keywords = "работа вакансии резюме банк ".mb_strtolower($current_city['city_name'],'utf-8');

require_once APP.HEADFILE;
?>
<script type="text/javascript" src="/files/js/bank_list.js"></script>
<h1>Вакансии банков <?php echo $current_city['city_name_rod']; ?></h1>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td align="left" valign="top" class="banki_main_col">
			<div class="golosovanie_info" id="vac_info"><?php
				if ($sort == "bank")
					echo "Отсортировано по банкам. Вы можете <a href='/$c_name/banks/vacancies/'>отсортировать по категориям</a> или <a href='?sort=list'>просмотреть все вакансии одним списком</a>";
				elseif ($sort == "list")
					echo "Показаны все вакансии. Вы можете <a href='?sort=bank'>отсортировать по банкам</a> или <a href='/$c_name/banks/vacancies/'>по категориям</a>";
				else
					echo "Отсортировано по категориям. Вы можете <a href='?sort=bank'>отсортировать по банкам</a> или <a href='?sort=list'>просмотреть все вакансии одним списком</a>";
			?></div>
			<div id="oprosy_table">
				<div>
					<table width="100%" border="0" cellpadding="8" cellspacing="0">
						<tr valign="top">
							<td width="40%"><?php
								if (mysql_num_rows($category_res) > 0)
								{
									?><table width="100%" border="0" cellpadding="8" cellspacing="0">
										<tr>
											<td width="24%"><strong><a href="?sort=list">Всего</a></strong></td>
											<td width="21%" align="left" valign="top">
												<strong><?php echo $all_vacancy_count; ?></strong>
											</td>
										</tr><?php
										while ($category_row = mysql_fetch_array($category_res))
										{
											?><tr>
												<td>
													<a href="<?php echo $link.$category_row['id'];?>"><?php echo $category_row['name']; ?></a>
												</td>
												<td width="21%" align="left" valign="top"><?php
													if ($sort == "list")
														echo $category_row['count'] != 0 ? "от ".get_salary($category_row['count'])." руб." : "по договоренности";
													else
														echo $category_row['count'];
												?></td>
											</tr><?php
										}
									?></table><?php
									require_once APP."pageskin.php";
								}
								else
									echo "<h3 class='gag'>На данный момент вакансий нет</h3>";
							?></td>
							<td><?php
								$fresh_vac_res = mysql_query("select `V`.`vacancy_id`,`V`.`title`,`V`.`salary`,`B`.`link`,`B`.`name` from `vacancy` as `V`,`bank` as `B` where `V`.`bank_id` = `B`.`bank_id` and `B`.`city_id` = '$_SESSION[city]' order by `V`.`vacancy_id` desc limit 4");
								if (mysql_num_rows($fresh_vac_res) > 0)
								{
									?><div class="vac_svezh_heading">Свежие вакансии</div>
									<table width="100%" cellpadding="0" cellspacing="0"><?php
									while ($fresh_vac_row = mysql_fetch_array($fresh_vac_res))
									{
										?><tr>
											<td class="vac_item">
												<a href="<?php echo "/$c_name";?>/banks/vacancies/<?php echo $fresh_vac_row['vacancy_id']; ?>"><?php
												echo $fresh_vac_row['title']; ?></a> &nbsp;&nbsp;
											</td>
											<td width="130px">
												<span class="vac_bank">
													<a href="<?php echo "/$c_name";?>/banks/<?php echo $fresh_vac_row['link'].".html"; ?>"><?php
													echo $fresh_vac_row['name']; ?></a>
												</span>
											</td>
										</tr>
										<tr>
											<td colspan="2">
												<div class="vac_salary"><?php
													echo $fresh_vac_row['salary'] != 0 ? "от ".get_salary($fresh_vac_row['salary'])." руб." : "по договоренности";
												?></div>
											</td>
										</tr><?php
									}
								}
							?></td>
						</tr>
					</table>
				</div>
			</div>
		</td>
		<td width="159" align="left" valign="top"><?php
			require_once APP."rating_column.php";
		?></td>
	</tr>
</table>
<div class="inside_block">
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr valign='top'>
			<td width="344" class="inside_info"><?php
				require_once APP."bank_list.php";
				require_once APP."currency_rates.php";
			?></td>
			<td width="646"><?php
				$info = mysql_fetch_array(mysql_query("select * from `info` where `info_id` = '4' limit 1"));
				$text = getRichText($info['text']);
				echo "<h2>$info[header]</h2>";
				echo "<p>$text</p>";
			?></td>
		</tr>
	</table>
</div>
<?php require_once APP.FOOTERFILE; ?>